package com.luzhikun.procinceAjax.dao;

import com.luzhikun.procinceAjax.db.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author Rookie Coder
 * @Description 关于省份业务实现类
 * @Date
 **/
public class PrivinceDao implements IPrivinceDao{
    private static JDBCUtil jdbcUtil=new JDBCUtil();
    @Override
    public String findPriInfo(int id) {
        String name="";
        // 创建连接对象
        Connection connection=null;
        // 获取预处理对象
        PreparedStatement pst=null;
        // 获取结果集
        ResultSet rs=null;
        // 创建连接
        try {
            connection=jdbcUtil.getConnection();
            // 编写sql
            String sql="select * from province where id=?";
            // 预处理对象编译sql
            pst=connection.prepareStatement(sql);
            // 动态替换占位符
            pst.setInt(1,id);
            // 获取结果集
            rs=pst.executeQuery();
            // 解析结果集
            if (rs!=null && rs.next() ){
                name=rs.getString("name");
            }
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        }
        // 关闭资源
        finally {
            try {
                jdbcUtil.closeAll(connection,pst,rs);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return name;
    }
}
